import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { environment } from '../../../../environments/environment';
//Base components
import { KeyIssuesModel } from '../../../shared/templates/keyIssues/keyIssues.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';

@Component({
    selector: 'key-issues',
    templateUrl: '../../../shared/templates/keyIssues/keyIssues.component.html',
    styleUrls: ['../../../shared/templates/keyIssues/keyIssues.component.css']
})
export class KeyIssuesSummaryComponent extends KeyIssuesModel {
    public subPeerGroup: string;
    public mei;
    public company;
    public clusterName;

    constructor(
        private riskService: CompanyRiskService,
        private http: Http) {
        super(http);

        this.currentComponent = "keyIssues";
        this.headerPercentWidth = 30;
        this.showLimit = true;
        this.columns = [
            {
                name: 'Weight',
                percentWidth: 20,
                maxRating: 20,
                valueField: 'finalWeight',
                highlight: false,
                chartColor: '#B3D4D1',
                showRating: true,
                boldRating: false,
                showChart: false,
                hasNoValue: true, //specified field is not an object with the value field
                formatMethod: this.formatMeiWeight, //format method
                ratingUnit: '%',
                limitImage: 'assets/img/icon-subindustry-avg.png',
                decimals: 2
            },
            {
                name: 'Exposure',
                percentWidth: 20,
                maxRating: 20,
                valueField: 'exposure',
                highlight: false,
                chartColor: '#B3D4D1',
                showRating: true,
                boldRating: false,
                showChart: false,
                ratingUnit: '',
                limitImage: 'assets/img/icon-subindustry-avg.png',
                decimals: 1
            },
            {
                name: 'Managed Risk',
                percentWidth: 20,
                maxRating: 20,
                valueField: 'managed',
                highlight: false,
                chartColor: '#B5B7B7',
                showRating: true,
                boldRating: false,
                showChart: false,
                ratingUnit: '',
                limitImage: 'assets/img/icon-subindustry-avg.png',
                decimals: 1
            },
            {
                name: 'Unmanaged Risk',
                percentWidth: 30,
                maxRating: 20,
                valueField: 'unmanaged',
                highlight: true,
                chartColor: 'black',
                showRating: true,
                boldRating: true,
                showChart: true,
                ratingUnit: '',
                limitImage: 'assets/img/icon-subindustry-avg.png',
                decimals: 1
            }
        ];
    }

    ngOnInit() {
        this.loaded = new Promise<void>((resolve, reject) => {
            this.riskService.company.then(company => {
                this.baseline = company.issues.baseline;
                this.elements = company.issues.others;
                this.subPeerGroup = company.subPeerGroup;
                this.company = company;
                resolve();
            });
        });

        this.getCompanyClusters().then(clusters => {
            this.riskService.company.then(company => {
                var cluster = clusters.filter(function (cluster) {
                    return cluster.code == company.assessmentCode;
                })[0];

                this.clusterName = cluster != undefined ? cluster.name : "N/A";
            });
        });
    }

    toggleIndicatorsDisplay(): void {
        this.showAllIndicators = !this.showAllIndicators;
    }
}